package com.qkl.xiatang.app.utils;

import com.qkl.xiatang.app.dto.user.UserOutDTO;
import com.qkl.xiatang.app.entity.user.AppUserInfo;
import com.qkl.xiatang.app.utils.appenum.AppUserDutiesEnum;
import com.qkl.xiatang.app.utils.appenum.AppUserStatus;

import java.util.Date;

public class AppSessionUtils {

    private static final ThreadLocal<AppUserSession> appThread = new ThreadLocal<>();

    public static  AppUserSession setUser(AppUserInfo user){
        AppUserSession session = null;
        if(null != user){
            session = new AppUserSession();
            session.setUser(user);
            session.setUserDTO(AppUserUtils.copyUserDTO(user));
            appThread.set(session);
        }
        return session;
    }

    public static AppUserSession getUser(){
        return appThread.get();
    }

    public static AppUserInfo getSessionUser(){
        AppUserSession session = getUser();
        if(null != session){
            return session.getUser();
        }
        return null;
    }

    public static UserOutDTO getSessionUserDTO(){
        AppUserSession session = getUser();
        if(null != session){
            return session.getUserDTO();
        }
        return null;
    }

    public static boolean isAdmin(){
        UserOutDTO userDTO = getSessionUserDTO();
        if(null != userDTO){
            return userDTO.isAdmin();
        }
        return false;
    }

    public static UserOutDTO getUserAppInfo(){
        return getSessionUserDTO();
    }

    public static boolean statusOK(){
        AppUserInfo userInfo = getSessionUser();
        if(null != userInfo){
            return AppUserStatus.isOK(userInfo.getStatus());
        }
        return false;
    }

    public static boolean statusApply(){
        AppUserInfo userInfo = getSessionUser();
        if(null != userInfo){
            return AppUserStatus.isApply(userInfo.getStatus());
        }
        return false;
    }


    public static String getCompanyId(){
        String companyId = "";
        AppUserInfo user = getSessionUser();
        if(null != user){
            companyId = user.getCompanyId();
        }
        return companyId;
    }

    public static String getUserId(){
        String userId = "";
        AppUserInfo user = getSessionUser();
        if(null != user){
            userId = user.getId();
        }
        return userId;
    }

    public static String getUserName(){
        String userName = "";
        AppUserInfo user = getSessionUser();
        if(null != user){
            userName = user.getName();
        }
        return userName;
    }

    public static Date getCreateTime(){
        Date createTime = null;
        AppUserInfo user = getSessionUser();
        if(null != user){
            createTime = user.getCreateTime();
        }
        return createTime;
    }

    public static String getIdentity(){
        String identity = "";
        AppUserInfo user = getSessionUser();
        if(null != user){
            identity = user.getIdentity();
        }
        return identity;
    }


    public static void removeUser(){
        appThread.remove();
    }




}
